'''
Created on 2012-5-29

@author: wb_suoyao.shen
'''
import datetime
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render_to_response
from ssyblog.diary.models import Diary
from ssyblog.tags.models import Tags
from ssyblog.comment.models import Comment
from ssyblog.comment.forms import CommentForm

def home(request):
    current_nav = 'home'
    #diary = Diary.diaryinfos.current_diary()
    diary = Diary.objects.latest_diary()
    comment_count = Comment.objects.get_comment_count(diary.id)
    recd_tags = Tags.objects.recod_tags_list('on', 4, 3)
    recently_list = Diary.objects.recently_diary()
    return render_to_response('index.html', locals())

def content(request,diaryId):
    current_nav = 'home'
    if diaryId:
        diary = Diary.objects.get(pk=diaryId)
    else:
        diary = Diary.objects.latest_diary()
    'here is comment query'
    comment_list = Comment.objects.filter(diary_id = diary.id)
    return render_to_response('content.html', locals())

@csrf_exempt
def saveComment(request):
    diary_id = ''
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            diary_id = obj.diary_id
            obj.comment_time = datetime.datetime.now()
            obj.client_ip = request.META['REMOTE_ADDR']
            obj.save()
    return content(request,diary_id)
    